//
//  CHDatePickerView.m
//  CarFile
//
//  Created by 陈浩 on 2018/1/23.
//  Copyright © 2018年 陈浩. All rights reserved.
//

#import "CHDatePickerView.h"

@interface CHDatePickerView()

/**
 *  工具条
 */
@property (nonatomic,weak) CZKeyboardTool *keyboardTool;
/**
 *  日期选择器
 */
@property (nonatomic,weak) UIDatePicker *startDatePicker;
@property (nonatomic,weak) UIDatePicker *endDatePicker;
/**
 *  遮盖按钮
 */
@property (nonatomic,strong) UIButton *cover;

@end
@implementation CHDatePickerView

/**
 *  创建日期选择器
 *
 */
+(instancetype)datePicker{
    return [[CHDatePickerView alloc] init];
}

- (id)initWithCoder:(NSCoder *)aDecoder {
    if (self = [super initWithCoder:aDecoder]) {
        [self setup];
    }
    return self;
}

- (instancetype)initWithFrame:(CGRect)frame {
    if (self = [super initWithFrame:frame]) {
        self.backgroundColor = [UIColor whiteColor];
        [self setup];
    }
    return self;
}

/**
 *  创建子控件
 */
- (void)setup{
    // 创建键盘工具条
    CZKeyboardTool *keyboardTool = [CZKeyboardTool keyboardTool];
    [self addSubview:keyboardTool];
    
    __weak typeof(self) weakSelf = self;
    keyboardTool.clickBlock = ^(CZKeyboardToolButtonType buttonType){
        // 隐藏工具条
        [weakSelf dismiss];
        if ([weakSelf.delegate respondsToSelector:@selector(datePicker:didClickBtnType:)]) {
            [weakSelf.delegate datePicker:weakSelf didClickBtnType:buttonType];
        }
        
    };
    self.keyboardTool = keyboardTool;
    
    // 创建日期选择器
    UIDatePicker *startDatePicker = [[UIDatePicker alloc] init];
    // 设置时区
    startDatePicker.locale = [NSLocale localeWithLocaleIdentifier:@"zh_CN"];
    // 设置时间模式
    startDatePicker.datePickerMode =   UIDatePickerModeDate;
    // 监听日期改变
    [startDatePicker addTarget:self action:@selector(dateValueChanged) forControlEvents:UIControlEventValueChanged];
    [self addSubview:startDatePicker];
    self.startDatePicker = startDatePicker;

    UIDatePicker *endDatePicker = [[UIDatePicker alloc] init];
    // 设置时区
    endDatePicker.locale = [NSLocale localeWithLocaleIdentifier:@"zh_CN"];
    // 设置时间模式
    endDatePicker.datePickerMode =   UIDatePickerModeDate;
    // 监听日期改变
    [endDatePicker addTarget:self action:@selector(dateValueChanged) forControlEvents:UIControlEventValueChanged];
    [self addSubview:endDatePicker];
    self.endDatePicker = endDatePicker;
    
    
}

/**
 *  监听日期改变的方法
 */
- (void)dateValueChanged{
    if ([self.delegate respondsToSelector:@selector(datePickerDidValueChanged:)]) {
        [self.delegate datePickerDidValueChanged:self];
    }
}

/**
 *  布局子控件
 */
-(void)layoutSubviews {
    [super layoutSubviews];
    
    CGRect frame = self.keyboardTool.frame;
    frame.size.width = self.frame.size.width;
    frame.size.height = 44;
    frame.origin.x = 0;
    frame.origin.y = 0;
    self.keyboardTool.frame = frame;
    
    CGFloat dateY = CGRectGetMaxY(self.keyboardTool.frame);
    CGFloat dateW = self.w/2;
    CGFloat dateH = 216;
    self.startDatePicker.frame = CGRectMake(0, dateY, dateW, dateH);
   
}

/**
 *  显示日期选择器
 */
-(void)show{
    // 获得窗口
    UIWindow *window = [UIApplication sharedApplication].keyWindow;
    // 获得窗口的宽高
    CGFloat windowH = CGRectGetHeight(window.frame);
    CGFloat windowW = CGRectGetWidth(window.frame);
    // 设置当前view的高度
    CGFloat height = 260;
    
    // 添加遮盖按钮
    self.cover.frame = CGRectMake(0, 0,windowW , windowH - height);
    [window addSubview:self.cover];
    
    self.frame = CGRectMake(0, windowH, windowW, height);
    [window addSubview:self];
    [UIView animateWithDuration:0.25 animations:^{
        CGRect frame = self.frame;
        frame.origin.y = windowH - height;
        self.frame = frame;
    }];
    
}
/**
 *  隐藏日期选择器
 */
-(void)dismiss{
    // 获得窗口
    UIWindow *window = [UIApplication sharedApplication].keyWindow;
    // 获得窗口的高
    CGFloat windowH = CGRectGetHeight(window.frame);
    [UIView animateWithDuration:0.25 animations:^{
        CGRect frame = self.frame;
        frame.origin.y = windowH;
        self.frame = frame;
    } completion:^(BOOL finished) {
        [self removeFromSuperview];
        // 移除遮盖
        [self.cover removeFromSuperview];
        _cover = nil;
    }];
}

- (void)dealloc {
    NSLog(@"dealloc---datePicker");
}

@end
